package com.neusoft.my12306.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.my12306.common.res.CommonResult;
import com.neusoft.my12306.entity.Station;
import com.neusoft.my12306.mapper.StationMapper;
import com.neusoft.my12306.service.IStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.my12306.utils.Pinyin4jCsdn;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 段静迪
 * @since 2023-07-06
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements IStationService {

    @Override
    public CommonResult addStation(Station station) {
        station.setSid(null);
        String  pinyin = Pinyin4jCsdn.getAllPinyin(station.getSnm());
        station.setSnmpy(pinyin);

        pinyin = Pinyin4jCsdn.getAllPinyin(station.getCity());
        station.setCitypy( pinyin );


        int i =  baseMapper.insert(station);

        CommonResult  result = new CommonResult(station.getSid());
        if (i <=0)
            result.setNotModified();

        return result;
    }

    @Override
    public CommonResult find4Auto(String v) {
        v =    "all".equals(v ) ? "" : v ;
        List<Station> stationList = baseMapper.find4Auto(v);

        CommonResult  result =new CommonResult(stationList);
        if (stationList==null ||  stationList.size()==0){
            result.setNotFound();
        }

        return result;
    }

    @Override
    public CommonResult loadByCity(String city) {
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("city" , city);
        List<Station> lss =  baseMapper.selectList(  wrapper);
        CommonResult  result = new CommonResult(lss);
        if (lss ==null ||  lss.size() == 0 )
            result.setNotFound();
        return result;
    }

    @Override
    public CommonResult modStation(Station station) {

        String  pinyin = Pinyin4jCsdn.getAllPinyin(station.getSnm());
        station.setSnmpy(pinyin);
        pinyin = Pinyin4jCsdn.getAllPinyin(station.getCity());
        station.setCitypy(pinyin);

        int i = baseMapper.updateById(station);
        CommonResult result =new CommonResult(i);
        if (i <=0)
            result.setNotModified();;
        return result;
    }
}
